package com.hoodbrains.insideloc.maps;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;

import com.hoodbrains.insideloc.R;
import com.hoodbrains.insideloc.maps.UI.CreationView;
import com.hoodbrains.insideloc.utils.Catalog;

public class CreateMapActivity extends Activity implements OnClickListener {
	Context mContext;
	Magasin magasin = null;
	CreationView view;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mContext = this;
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.creation_map);
		view = (CreationView) findViewById(R.id.map_creation_test);
		Button add_vert = (Button) findViewById(R.id.add_vertical);
		Button add_hor = (Button) findViewById(R.id.add_horizontal);
		ImageButton zoomPlus = (ImageButton) findViewById(R.id.zoom_ctrl_plus);
		ImageButton zoomMinus = (ImageButton) findViewById(R.id.zoom_ctrl_minus);
		ImageButton delete_gondole = (ImageButton) findViewById(R.id.delete_gondole);
		add_vert.setOnClickListener(this);
		add_hor.setOnClickListener(this);
		zoomPlus.setOnClickListener(this);
		zoomMinus.setOnClickListener(this);
		delete_gondole.setOnClickListener(this);
		findViewById(R.id.edit_gondole).setOnClickListener(this);
		findViewById(R.id.save).setOnClickListener(this);

		// view.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
		// LayoutParams.FILL_PARENT));
		// view.setBackgroundColor(0xFFFFFFFF);
//		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//		DocumentBuilder db = null;
//
//		try {
//			db = dbf.newDocumentBuilder();
//			Document doc = db.parse(getAssets().open("supermarket.xml"));
		Document doc = Catalog.getInstance().getMap(
						Catalog.getInstance().getCurrentSuperMarketID());
			Element magasinElem = doc.getDocumentElement();
			magasin = Magasin.deserialize(magasinElem);
			view.setSupermarket(magasin);
//		} catch (Exception e) {
//			// TODO: handle exception
//		}
		
		registerForContextMenu(view);

	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.add_vertical: {
			Gondole gondole = new Gondole();
			gondole.setVertical(true);
			gondole.setHeight(500);
			gondole.setX(100);
			gondole.setY(100);
			gondole.setWidth(100);
			gondole.getCote1().add(new Rayon("Empty"));
			gondole.getCote2().add(new Rayon("Empty"));
			magasin.addGondole(gondole);

			view.invalidate();

			break;
		}
		case R.id.add_horizontal: {
			Gondole gondole = new Gondole();
			gondole.setVertical(false);
			gondole.setHeight(100);
			gondole.setWidth(500);
			gondole.setX(100);
			gondole.setY(100);
			gondole.getCote1().add(new Rayon("Empty"));
			gondole.getCote2().add(new Rayon("Empty"));
			magasin.addGondole(gondole);
			view.invalidate();
			break;
		}
		case R.id.zoom_ctrl_minus: {
			view.incrementZoom();
			view.invalidate();
			break;
		}
		case R.id.zoom_ctrl_plus: {
			view.decrementZoom();
			view.invalidate();
			break;
		}
		case R.id.delete_gondole: {
			if (view.getSelectedGondole() != null){
				magasin.removeGondole(view.getSelectedGondole());
			
			}
			view.invalidate();
			break;
		}
		
		case R.id.edit_gondole : {
			if (view.getSelectedGondole() != null){
			Catalog.getInstance().setCurrentCondole(view.getSelectedGondole());
			Intent intent = new Intent(mContext, EditGondoleActivity.class);
			startActivity(intent);
			}
			break;
		}
		
		case R.id.save : {
			saveToFile();
			
			break;
		}
		}

	}

	private void saveToFile() {
		//Trouver un objet document.
		Document doc = Catalog.getInstance().newDocument();
		//Y serialiser le magasin.
		doc.appendChild(magasin.serialize(doc));
		
		//l'ecrire sur la sd.
		Catalog.getInstance().saveXMLMap(Catalog.getInstance().getCurrentSuperMarketID(), doc);
		
	}

}
